package com.jeff.tianti.common.SMS;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.jeff.tianti.common.enmus.SmsTemplate;
import com.aliyuncs.exceptions.ServerException;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.aliyuncs.http.MethodType;

import java.security.SecureRandom;

@Component
public class SmsSendUtils {

    @Bean
    public IAcsClient acsClient() throws ClientException {
        DefaultProfile profile =
                DefaultProfile.getProfile(
                        SmsTemplate.regionId.getMsg(),
                        SmsTemplate.accessKeyId.getMsg(),
                        SmsTemplate.secret.getMsg());
        DefaultProfile.addEndpoint(
                SmsTemplate.endpointName.getMsg(),
                SmsTemplate.regionId.getMsg(),
                SmsTemplate.PRODUCT.getMsg(),
                SmsTemplate.domain.getMsg());
        return new DefaultAcsClient(profile);
    }


    public boolean sendJFSCbuyer(SmsTemplate templatecode, String phoneNumber, String jsoncode) {
        DefaultProfile profile =
                DefaultProfile.getProfile(
                        "cn-hangzhou", SmsTemplate.accessKeyId.getMsg(), SmsTemplate.secret.getMsg());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        // request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        // request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("RegionId", SmsTemplate.regionId.getMsg());
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SmsTemplate.SignName.getMsg());
        request.putQueryParameter("TemplateCode", templatecode.getMsg());


        request.putQueryParameter("TemplateParam", jsoncode);
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }

    public String getVerificationCode(String phoneNumber) {
        DefaultProfile profile =
                DefaultProfile.getProfile(
                        "cn-hangzhou", SmsTemplate.accessKeyId.getMsg(), SmsTemplate.secret.getMsg());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        // request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        // request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("RegionId", SmsTemplate.regionId.getMsg());
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SmsTemplate.SignName.getMsg());
        request.putQueryParameter("TemplateCode", SmsTemplate.TemplateCode_register.getMsg());

        SecureRandom random = new SecureRandom();
        String code = String.valueOf(random.nextInt(899999));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        // request.putQueryParameter("TemplateParam", "{\"code\":\"88888888\"}");
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            // System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return code;
    }
}
